import ServerConfig from "../server.json";
import { ResultHandler } from './middleware';

const COMMUNITY_ID_PROPERTY_NAME = "_covidCommunityId";
const LIST_ID_PROPERTY_NAME = "_covidListId";

const baseUrl = ServerConfig.baseUrl;
const apis = ServerConfig.apis;

const managerAPIs = {
    async login(data) {
        const result = await uni.request({
            url: baseUrl + apis.communityLogin.url,
            method: apis.communityLogin.method,
            data: data,
            timeout: 10000
        });
        return ResultHandler(result);
    },
    async register(data) {
        const result = await uni.request({
            url: baseUrl + apis.communityRegister.url,
            method: apis.communityRegister.method,
            data: data,
            header: { token: managerAPIs.getCommunityIdentifier() },
            timeout: 10000
        });
        return ResultHandler(result);
    },
    cancel() {},
    async getDetail(id) {
        const result = await uni.request({
            url: baseUrl + apis.getCommunityDetail.url,
            method: apis.getCommunityDetail.method,
            data: { id },
            header: { token: managerAPIs.getCommunityIdentifier() },
            timeout: 10000
        });
        return ResultHandler(result);
    },
    async getDetailByUser(id) {
        const result = await uni.request({
            url: baseUrl + apis.getCommunityDetailByUser.url,
            method: apis.getCommunityDetailByUser.method,
            data: { id },
            timeout: 10000
        });
        return ResultHandler(result);
    },
    async scan(id) {
        const result = await uni.request({
            url: baseUrl + apis.communityScan.url,
            method: apis.communityScan.method,
            data: { uid: id },
            header: { token: managerAPIs.getCommunityIdentifier() },
            timeout: 10000
        });
        return ResultHandler(result);
    },
    getListIdentifier() {
        return localStorage.getItem(LIST_ID_PROPERTY_NAME);
    },
    setListIdentifier(id) {
        localStorage.setItem(LIST_ID_PROPERTY_NAME, id);
    },
    clearListIdentifier() {
        localStorage.removeItem(LIST_ID_PROPERTY_NAME);
    },
    getCommunityIdentifier() {
        return localStorage.getItem(COMMUNITY_ID_PROPERTY_NAME);
    },
    setCommunityIdentifier(id) {
        localStorage.setItem(COMMUNITY_ID_PROPERTY_NAME, id);
    },
    clearCommunityIdentifier() {
        localStorage.removeItem(COMMUNITY_ID_PROPERTY_NAME);
    }
};

export default managerAPIs;